Skip to content

Add min/max file size limits by mime type. #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

willvincent
Copy link

Closes issue #3.

Adds optional mimeMinFileSizes and mimeMaxFileSizes config options, these should work in conjunction with existing min/max and max total limits, and allow for setting min or max limits by explicit mime type or mime type wildcard group:

ie:

<script>
        // Register the plugin with FilePond
        FilePond.registerPlugin(FilePondPluginFileValidateSize);

        // Get a reference to the file input element
        const inputElement = document.querySelector('input[type="file"]');

        // Create the FilePond instance
        const pond = FilePond.create(inputElement, {
            mimeMaxFileSizes: {
                'image/*': '3MB',
                'image/gif': '5MB',
                'video/mp4': '100MB'
            }
        });
</script>

That config will allow images of any type up to 3mb, but allow gifs to be up to 5mb, and restrict mp4 videos to 100MB.

@rikschennink
Copy link
Collaborator

Appreciate the PR, will consider for v5

if (mimeMaxFileSizes !== null) {
Object.keys(mimeMaxFileSizes).forEach(mime => {
if (file.type === mime ||
(mime.substring(2, -2) === '/*'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(mime.substring(2, -2) === '/*'
(mime.substring(mime.length-2) === '/*'

if (mimeMinFileSizes !== null) {
Object.keys(mimeMinFileSizes).forEach(mime => {
if (file.type === mime ||
(mime.substring(2, -2) === '/*'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(mime.substring(2, -2) === '/*'
(mime.substring(mime.length-2) === '/*'


// Max individual file size in bytes
maxFileSize: [null, Type.INT],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

// Multipliers should probably be 1024 vs 1000,
// but this mimics the internal behavior.
const multiplier = { b: 1, kb: 1000, mb: 1000**2, gb: 1000**3, tb: 1000**4, pb: 1000**5 }
const num = Number(str.match(/\d/).toString())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const num = Number(str.match(/\d/).toString())
const num = Number(str.match(/\d+/).toString())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works better for values like 1024KB. For example, see the different max sizes in https://community.sinch.com/t5/MMS/What-is-the-maximum-size-of-MMS-message-that-you-can-send/ta-p/7847

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants